package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.entity.Myorder;
import com.chixing.mapper.MyorderMapper;
import com.chixing.service.IMyorderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author smith
 * @since 2025-05-21
 */
@Service
public class MyorderServiceImpl  implements IMyorderService {
    @Autowired
    private MyorderMapper myorderMapper;

    @Override
    public Result getById(Integer myorderId) {
        Myorder myorder =myorderMapper.selectById(myorderId);
        if(myorder != null)
            return Result.getSuccess(myorder);
        return Result.getFail(null);
    }

    @Override
    public Result save(Myorder myorder) {
        myorder.setOrderNumber(UUID.randomUUID().toString().replace("-",""));
        myorder.setCustId(1);
        myorder.setOrderCreateTime(LocalDateTime.now());
        myorder.setStatus("0");  // 已下单，未支付

        System.out.println("MyorderServiceImpl 订单信息是：" + myorder)   ;
        int rows = myorderMapper.insert(myorder);
        if(rows >0)
            return Result.saveSuccess(myorder);
        return  Result.saveFail(myorder);
    }

    @Override
    public Result getOrdersByUserId(Integer userId) {
        QueryWrapper<Myorder> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_id",userId);
        List<Myorder> orderList = myorderMapper.selectList(wrapper);
        if(orderList !=null && orderList.size() >0)
            return Result.getSuccess(orderList);
        else
            return Result.getFail("没有订单");
    }
}
